package engine.entities;

import java.util.LinkedList;

import engine.colliders.CollisionResolver;
import engine.controllers.Controller;
import engine.controllers.Null;
import engine.physics.PhysicsResolver;
import engine.render.Sprite;

public class Entity {
	Spatial spatial;
	Sprite sprite;
	Energy energy;
	Controller controller;
	Physical physical;
	LinkedList<CollisionResolver> colliders;
	LinkedList<PhysicsResolver> physics;

	public Entity() {
		controller = new Null();
		colliders = new LinkedList<CollisionResolver>();
		physics = new LinkedList<PhysicsResolver>();
		energy = new Energy(0);
		spatial = new Spatial();
		physical = new Physical();
	}

	public Spatial getSpatial() {
		return this.spatial;
	}

	public void setSpatial(Spatial spatial) {
		this.spatial = spatial;
	}

	public Controller getController() {
		return this.controller;
	}

	public void setController(Controller controller) {
		this.controller = controller;
	}

	public Energy getEnergy() {
		return this.energy;
	}

	public void setEnergy(Energy energy) {
		this.energy = energy;
	}

	public Sprite getSprite() {
		return this.sprite;
	}

	public void setSprite(Sprite sprite) {
		this.sprite = sprite;
		this.spatial.setWidth(sprite.getWidth());
		this.spatial.setHeight(sprite.getHeight());
	}

	public boolean removeCollider(CollisionResolver collider) {
		return colliders.remove(collider);
	}

	public boolean addCollider(CollisionResolver collider) {
		return colliders.add(collider);
	}
	
	public Physical getPhysical() {
		return this.physical;
	}

	public void setPhysical(Physical physical) {
		this.physical = physical;
	}

	public boolean removePhysics(PhysicsResolver physics) {
		return this.physics.remove(physics);
	}

	public boolean addPhysics(PhysicsResolver physics) {
		return this.physics.add(physics);
	}

	public void updatePhysics(float delta) {
		for (PhysicsResolver solver : physics)
			solver.updatePhysics(this,delta);
	}

	public void updateCollisions(Entity e) {
		for (CollisionResolver solver : colliders) {
			if (solver.precondMet(this, e))
				solver.updateCollision(this, e);
		}
	}

	public void updateController(float delta) {
		controller.updateController(this,delta);
	}

	public void updatePosition() {		 
		spatial.x += spatial.dx;
		spatial.y += spatial.dy;
	}
}
